package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * @author xupkun
 * @date 2025/4/6
 */
@Slf4j
@LiteflowComponent(value = "CallOtherTask", name = "[轮对/轮轴]呼叫其他AGV任务")
@RequiredArgsConstructor
public class CallOtherTask extends NodeComponent {

    private final RestTemplate restTemplate;

    @Override
    public void process() throws Exception {
        String uri = this.getCmpData(JSONObject.class).get(FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0), String.class).trim();
        FlowContextDto dto =  this.getRequestData();
        FlowPatrolPreAndEndTicketsDto ticketsDto = com.alibaba.fastjson.JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String pjData = String.valueOf(ticketsDto.getModalData().get("pjData"));
        if (StrUtil.isNotBlank(pjData)) {
            try {
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(pjData);
                // 循环获取 JSON 的 key
                for (String key : jsonObject.keySet()) {
                    uri = uri + "&" + key + "=" + jsonObject.getString(key);
                }
            } catch (Exception e) {
                log.error("解析 JSON 数据时发生异常: {}", e.getMessage(), e);
            }
        }
        String result = restTemplate.postForObject(uri, "", String.class);
        log.info("呼叫其他的任务是 {}---结果是:{}", uri, result);
    }
}
